/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.InOutDetailBean;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessProfit;
import com.minierp.model.CooperBill;
import com.minierp.model.CooperBillItem;
import com.minierp.model.Establishment;
import com.minierp.model.FeeBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessProfitDetailExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessProfitDetail.xls\"");
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(10);
		font.setColor(10);
		font.setFontName("黑体");
		font.setBoldweight(700);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(2);
		cellStyle.setBorderBottom(1);
		cellStyle.setWrapText(true);
		cellStyle.setFillForegroundColor(22);
		cellStyle.setFillPattern(1);
		HSSFSheet sheet = workbook.createSheet("list");
		BusinessBill bill = (BusinessBill) model.get("bill");
		BusinessBillExt be = (BusinessBillExt) model.get("be");
		BusinessProfit profit = (BusinessProfit) model.get("profit");
		Establishment est = (Establishment) model.get("est");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		BigDecimal remainCost = bill.getCost().add(be.getAdjustCost())
				.subtract(be.getCollectedCost());
		BigDecimal cutCost = profit.getMaterialCost()
				.add(profit.getCooperCost()).add(profit.getFeeCost());
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFFont normalFont = workbook.createFont();
		HSSFCellStyle norDataCellStyle = workbook.createCellStyle();
		norDataCellStyle.setBorderBottom(1);
		norDataCellStyle.setBorderTop(1);
		norDataCellStyle.setBorderLeft(1);
		norDataCellStyle.setBorderRight(1);
		HSSFCellStyle boldCellStyle = workbook.createCellStyle();
		boldCellStyle.setFont(boldFont);
		boldCellStyle.setBorderBottom(1);
		boldCellStyle.setBorderTop(1);
		boldCellStyle.setBorderLeft(1);
		boldCellStyle.setBorderRight(1);
		byte index = 0;
		byte colNum = 9;
		HSSFRow r = sheet.createRow(index);
		r.setHeight(600);
		HSSFCell c = r.createCell(0);

		for (int ts = 1; ts < colNum; ++ts) {
			r.createCell((short) ts);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString("业务单成本表"));
		sheet.addMergedRegion(new Region(index, 0, index, (short) (colNum - 1)));
		int arg34 = index + 1;
		r = sheet.createRow(arg34);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		HSSFRichTextString arg35 = new HSSFRichTextString("客户名称："
				+ est.getEstName());
		arg35.applyFont(0, 5, boldFont);
		arg35.applyFont(5, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 0, arg34, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg35 = new HSSFRichTextString("业务单号：" + bill.getBillCode());
		arg35.applyFont(0, 5, boldFont);
		arg35.applyFont(5, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 3, arg34, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		arg35 = new HSSFRichTextString("订货日期：" + sf.format(bill.getOrderDate()));
		arg35.applyFont(0, 5, boldFont);
		arg35.applyFont(5, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 6, arg34, 8));
		++arg34;
		r = sheet.createRow(arg34);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		arg35 = new HSSFRichTextString("合同金额：" + bill.getCost());
		arg35.applyFont(0, 5, boldFont);
		arg35.applyFont(5, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 0, arg34, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		arg35 = new HSSFRichTextString("摘要：" + bill.getContent());
		arg35.applyFont(0, 3, boldFont);
		arg35.applyFont(3, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 3, arg34, 8));
		++arg34;
		r = sheet.createRow(arg34);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		arg35 = new HSSFRichTextString("已调账：" + be.getAdjustCost());
		arg35.applyFont(0, 4, boldFont);
		arg35.applyFont(4, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 0, arg34, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg35 = new HSSFRichTextString("已收金额：" + be.getCollectedCost());
		arg35.applyFont(0, 5, boldFont);
		arg35.applyFont(5, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 3, arg34, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		arg35 = new HSSFRichTextString("欠款：" + remainCost);
		arg35.applyFont(0, 3, boldFont);
		arg35.applyFont(3, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 6, arg34, 8));
		++arg34;
		r = sheet.createRow(arg34);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		arg35 = new HSSFRichTextString("成本合计：" + cutCost);
		arg35.applyFont(0, 5, boldFont);
		arg35.applyFont(5, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 0, arg34, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg35 = new HSSFRichTextString("毛利率：" + model.get("percent") + "%");
		arg35.applyFont(0, 4, boldFont);
		arg35.applyFont(4, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 3, arg34, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		arg35 = new HSSFRichTextString("毛利：" + profit.getProfitCost());
		arg35.applyFont(0, 3, boldFont);
		arg35.applyFont(3, arg35.length(), normalFont);
		c.setCellValue(arg35);
		sheet.addMergedRegion(new Region(arg34, 6, arg34, 8));
		++arg34;
		r = sheet.createRow(arg34);
		c = r.createCell(0);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(1);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(2);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("供/需方"));
		c = r.createCell(3);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("出/入"));
		c = r.createCell(4);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("材料编号"));
		c = r.createCell(5);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("品名规格"));
		c = r.createCell(6);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(7);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(8);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		++arg34;
		List ioList = (List) model.get("ioList");

		int coopList;
		for (coopList = 0; coopList < ioList.size(); ++coopList) {
			InOutDetailBean mtList = (InOutDetailBean) ioList.get(coopList);
			String depList = "";
			BigDecimal ftList;
			BigDecimal feeList;
			if (mtList.getBillType().equals("IN")) {
				depList = "入";
				ftList = mtList.getInAmount();
				feeList = mtList.getInCost();
			} else {
				depList = "出";
				ftList = mtList.getOutAmount();
				feeList = mtList.getOutCost();
			}

			r = sheet.createRow(coopList + arg34);
			c = r.createCell(0);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(sf.format(mtList
					.getBillDate())));
			c = r.createCell(1);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(mtList.getBillCode()));
			c = r.createCell(2);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(mtList.getOtherName()));
			c = r.createCell(3);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(depList));
			c = r.createCell(4);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(mtList.getProductCode()));
			c = r.createCell(5);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(mtList.getProductName()));
			c = r.createCell(6);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(mtList.getProductUnit()));
			c = r.createCell(7);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(ftList.doubleValue());
			c = r.createCell(8);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(feeList.doubleValue());
		}

		arg34 += ioList.size();
		r = sheet.createRow(arg34);
		c = r.createCell(0);

		for (coopList = 1; coopList < colNum; ++coopList) {
			r.createCell((short) coopList);
		}

		sheet.addMergedRegion(new Region(arg34, 0, arg34, (short) (colNum - 1)));
		++arg34;
		r = sheet.createRow(arg34);
		c = r.createCell(0);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(1);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(2);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("项目类型"));
		c = r.createCell(3);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("内容"));
		c = r.createCell(4);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("材质"));
		c = r.createCell(5);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("规格"));
		c = r.createCell(6);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("制作要求"));
		c = r.createCell(7);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(8);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(9);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(10);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		++arg34;
		List arg36 = (List) model.get("coopList");
		List arg37 = (List) model.get("mtList");

		int arg38;
		for (arg38 = 0; arg38 < arg36.size(); ++arg38) {
			Object[] arg39 = (Object[]) arg36.get(arg38);
			CooperBillItem arg42 = (CooperBillItem) arg39[1];
			CooperBill os = (CooperBill) arg39[0];
			String objs = "";
			BigDecimal item;
			if (arg42.getPriceType().equals("AREA")) {
				objs = "平方米";
				item = arg42.getSquareNum();
			} else if (arg42.getPriceType().equals("LENGTH")) {
				if (arg42.getLengType().equals("M")) {
					objs = "米";
				} else {
					objs = "公分";
				}

				item = arg42.getSquareNum();
			} else {
				objs = arg42.getUnit();
				item = arg42.getAmount();
			}

			r = sheet.createRow(arg38 + arg34);
			c = r.createCell(0);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(sf.format(os.getCooperDate())));
			c = r.createCell(1);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(os.getBillCode()));
			c = r.createCell(2);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(arg37, arg42.getMakeTypeId())));
			c = r.createCell(3);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(arg42.getFinishedContent()));
			c = r.createCell(4);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(arg42.getProName()));
			c = r.createCell(5);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(arg42.getSpecification()));
			c = r.createCell(6);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(arg42.getMakeRequire()));
			c = r.createCell(7);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(objs));
			c = r.createCell(8);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(item.doubleValue());
			c = r.createCell(9);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(arg42.getUnitPrice().doubleValue());
			c = r.createCell(10);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(arg42.getCost().doubleValue());
		}

		arg34 += arg36.size();
		r = sheet.createRow(arg34);
		c = r.createCell(0);

		for (arg38 = 1; arg38 < colNum; ++arg38) {
			r.createCell((short) arg38);
		}

		sheet.addMergedRegion(new Region(arg34, 0, arg34, (short) (colNum - 1)));
		++arg34;
		r = sheet.createRow(arg34);
		c = r.createCell(0);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(1);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(2);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("费用类型"));
		c = r.createCell(3);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		c = r.createCell(4);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("备注"));
		c = r.createCell(5);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("经办人"));
		c = r.createCell(6);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("部门"));
		++arg34;
		List arg41 = (List) model.get("depList");
		List arg40 = (List) model.get("ftList");
		List arg43 = (List) model.get("feeList");

		for (int arg44 = 0; arg44 < arg43.size(); ++arg44) {
			Object[] arg46 = (Object[]) arg43.get(arg44);
			FeeBillItem arg47 = (FeeBillItem) arg46[0];
			r = sheet.createRow(arg44 + arg34);
			c = r.createCell(0);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(sf.format((Date) arg46[1])));
			c = r.createCell(1);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString((String) arg46[2]));
			c = r.createCell(2);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(arg40, arg47.getFtId())));
			c = r.createCell(3);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(arg47.getCost().doubleValue());
			c = r.createCell(4);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(arg47.getMark()));
			c = r.createCell(5);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString((String) arg46[3]));
			c = r.createCell(6);
			c.setCellStyle(norDataCellStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(arg41, ((Long) arg46[4]).longValue())));
		}

		int arg9999 = arg34 + arg43.size();
		sheet.setColumnWidth(0, 3200);
		sheet.setColumnWidth(1, 5000);
		ServletOutputStream arg45 = response.getOutputStream();
		workbook.write(arg45);
		arg45.flush();
		arg45.close();
	}
}